//
//  DMUserConsent.m
//  DualMediationSDK
//
//  Created by AI Assistant
//  Copyright © 2024 YourCompany. All rights reserved.
//

#import "DMUserConsent.h"

@interface DMUserConsent ()

@property (nonatomic, strong, readwrite) NSDate *createdAt;
@property (nonatomic, strong, readwrite) NSDate *updatedAt;
@property (nonatomic, strong, readwrite) NSString *version;
@property (nonatomic, strong, readwrite) NSArray<NSDictionary *> *consentHistory;

@end

@implementation DMUserConsent

+ (BOOL)supportsSecureCoding {
    return YES;
}

#pragma mark - Initialization

- (instancetype)init {
    self = [super init];
    if (self) {
        _createdAt = [NSDate date];
        _updatedAt = [NSDate date];
        _version = @"1.0.0";
        _consentHistory = @[];
        
        // 初始化所有同意状态为未知
        _attStatus = DMConsentStatusUnknown;
        _gdprStatus = DMConsentStatusUnknown;
        _ccpaStatus = DMConsentStatusUnknown;
        _umpStatus = DMConsentStatusUnknown;
        _coppaStatus = DMConsentStatusUnknown;
        
        // 初始化布尔值属性
        _canCollectData = NO;
        _canShowPersonalizedAds = NO;
        _canShareData = NO;
        _canCollectAnalytics = NO;
        _canTrackAcrossApps = NO;
        _gdprApplies = NO;
        _ccpaApplies = NO;
        _isInGDPRRegion = NO;
        _isInCCPARegion = NO;
        _isChildUser = NO;
        _requiresParentalConsent = NO;
    }
    return self;
}

+ (instancetype)defaultConsent {
    return [[self alloc] init];
}

#pragma mark - NSCoding

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        _userId = [coder decodeObjectOfClass:[NSString class] forKey:@"userId"];
        _createdAt = [coder decodeObjectOfClass:[NSDate class] forKey:@"createdAt"];
        _updatedAt = [coder decodeObjectOfClass:[NSDate class] forKey:@"updatedAt"];
        _version = [coder decodeObjectOfClass:[NSString class] forKey:@"version"];
        
        _attStatus = [coder decodeIntegerForKey:@"attStatus"];
        _gdprStatus = [coder decodeIntegerForKey:@"gdprStatus"];
        _ccpaStatus = [coder decodeIntegerForKey:@"ccpaStatus"];
        _umpStatus = [coder decodeIntegerForKey:@"umpStatus"];
        _coppaStatus = [coder decodeIntegerForKey:@"coppaStatus"];
        
        _canCollectData = [coder decodeBoolForKey:@"canCollectData"];
        _canShowPersonalizedAds = [coder decodeBoolForKey:@"canShowPersonalizedAds"];
        _canShareData = [coder decodeBoolForKey:@"canShareData"];
        _canCollectAnalytics = [coder decodeBoolForKey:@"canCollectAnalytics"];
        _canTrackAcrossApps = [coder decodeBoolForKey:@"canTrackAcrossApps"];
        
        _gdprApplies = [coder decodeBoolForKey:@"gdprApplies"];
        _gdprConsentString = [coder decodeObjectOfClass:[NSString class] forKey:@"gdprConsentString"];
        _gdprConsentDate = [coder decodeObjectOfClass:[NSDate class] forKey:@"gdprConsentDate"];
        
        _ccpaApplies = [coder decodeBoolForKey:@"ccpaApplies"];
        _ccpaOptOutDate = [coder decodeObjectOfClass:[NSDate class] forKey:@"ccpaOptOutDate"];
        
        _countryCode = [coder decodeObjectOfClass:[NSString class] forKey:@"countryCode"];
        _stateCode = [coder decodeObjectOfClass:[NSString class] forKey:@"stateCode"];
        _isInGDPRRegion = [coder decodeBoolForKey:@"isInGDPRRegion"];
        _isInCCPARegion = [coder decodeBoolForKey:@"isInCCPARegion"];
        
        _userAge = [coder decodeObjectOfClass:[NSNumber class] forKey:@"userAge"];
        _isChildUser = [coder decodeBoolForKey:@"isChildUser"];
        _requiresParentalConsent = [coder decodeBoolForKey:@"requiresParentalConsent"];
        
        _consentHistory = [coder decodeObjectOfClasses:[NSSet setWithObjects:[NSArray class], [NSDictionary class], [NSString class], [NSDate class], [NSNumber class], nil] forKey:@"consentHistory"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.userId forKey:@"userId"];
    [coder encodeObject:self.createdAt forKey:@"createdAt"];
    [coder encodeObject:self.updatedAt forKey:@"updatedAt"];
    [coder encodeObject:self.version forKey:@"version"];
    
    [coder encodeInteger:self.attStatus forKey:@"attStatus"];
    [coder encodeInteger:self.gdprStatus forKey:@"gdprStatus"];
    [coder encodeInteger:self.ccpaStatus forKey:@"ccpaStatus"];
    [coder encodeInteger:self.umpStatus forKey:@"umpStatus"];
    [coder encodeInteger:self.coppaStatus forKey:@"coppaStatus"];
    
    [coder encodeBool:self.canCollectData forKey:@"canCollectData"];
    [coder encodeBool:self.canShowPersonalizedAds forKey:@"canShowPersonalizedAds"];
    [coder encodeBool:self.canShareData forKey:@"canShareData"];
    [coder encodeBool:self.canCollectAnalytics forKey:@"canCollectAnalytics"];
    [coder encodeBool:self.canTrackAcrossApps forKey:@"canTrackAcrossApps"];
    
    [coder encodeBool:self.gdprApplies forKey:@"gdprApplies"];
    [coder encodeObject:self.gdprConsentString forKey:@"gdprConsentString"];
    [coder encodeObject:self.gdprConsentDate forKey:@"gdprConsentDate"];
    
    [coder encodeBool:self.ccpaApplies forKey:@"ccpaApplies"];
    [coder encodeObject:self.ccpaOptOutDate forKey:@"ccpaOptOutDate"];
    
    [coder encodeObject:self.countryCode forKey:@"countryCode"];
    [coder encodeObject:self.stateCode forKey:@"stateCode"];
    [coder encodeBool:self.isInGDPRRegion forKey:@"isInGDPRRegion"];
    [coder encodeBool:self.isInCCPARegion forKey:@"isInCCPARegion"];
    
    [coder encodeObject:self.userAge forKey:@"userAge"];
    [coder encodeBool:self.isChildUser forKey:@"isChildUser"];
    [coder encodeBool:self.requiresParentalConsent forKey:@"requiresParentalConsent"];
    
    [coder encodeObject:self.consentHistory forKey:@"consentHistory"];
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone {
    DMUserConsent *copy = [[DMUserConsent alloc] init];
    
    copy.userId = [self.userId copy];
    copy->_createdAt = [self.createdAt copy];
    copy->_updatedAt = [self.updatedAt copy];
    copy->_version = [self.version copy];
    
    copy.attStatus = self.attStatus;
    copy.gdprStatus = self.gdprStatus;
    copy.ccpaStatus = self.ccpaStatus;
    copy.umpStatus = self.umpStatus;
    copy.coppaStatus = self.coppaStatus;
    
    copy.canCollectData = self.canCollectData;
    copy.canShowPersonalizedAds = self.canShowPersonalizedAds;
    copy.canShareData = self.canShareData;
    copy.canCollectAnalytics = self.canCollectAnalytics;
    copy.canTrackAcrossApps = self.canTrackAcrossApps;
    
    copy.gdprApplies = self.gdprApplies;
    copy.gdprConsentString = [self.gdprConsentString copy];
    copy.gdprConsentDate = [self.gdprConsentDate copy];
    
    copy.ccpaApplies = self.ccpaApplies;
    copy.ccpaOptOutDate = [self.ccpaOptOutDate copy];
    
    copy.countryCode = [self.countryCode copy];
    copy.stateCode = [self.stateCode copy];
    copy.isInGDPRRegion = self.isInGDPRRegion;
    copy.isInCCPARegion = self.isInCCPARegion;
    
    copy.userAge = [self.userAge copy];
    copy.isChildUser = self.isChildUser;
    copy.requiresParentalConsent = self.requiresParentalConsent;
    
    copy->_consentHistory = [self.consentHistory copy];
    
    return copy;
}

@end